package dk;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


// The purpose of this is to scan a target directory and build a list of all c files and their methods.
//	This information will be used to determine the "home" directory for the crest output.



public class getAllCMethods {
	
	util u = new util();
	
	private List<String> filelist=new ArrayList<String>();
	private List<ctag_fileInfo>ctagfileInfoList=new ArrayList<ctag_fileInfo>();
		
	public static void main(String[] args) {
		getAllCMethods gac = new getAllCMethods();
		gac.Run();
	}
	
	
	public List<ctag_fileInfo> getCompleteMethodList(String inputPath, String extension){
	
		
		List<ctag_fileInfo> localCtagList =new ArrayList<ctag_fileInfo>();
		String tempLineInfo="";
		String className="";
		String tempMethodName="";
		
		listFilesAndFilesSubDirectories(inputPath,extension);
		for(int i=0;i<filelist.size();i++){
			
			className = u.getFileNameFromInputPath(filelist.get(i)).replace(extension, "");
		
		//	System.out.println("xxxxxxxxxxxxxxxxxxHere: " + filelist.get(i));
			
			for(int z=0; z<u.getFileLineCount(filelist.get(i));z++){
				tempLineInfo = u.Readfile(filelist.get(i), z);	
				if(isCtagLineMethod(tempLineInfo)){
					tempMethodName = getMethodFromLine(tempLineInfo);
					System.out.println("Ctag Add: " + className +" " + tempMethodName);
					localCtagList.add(new ctag_fileInfo(className, tempMethodName));
				}	
			}

		}

		return localCtagList;
	}
	
	
	
	

	
	
	
	public void Run(){
		
		
		

		
		//System.out.println(filelist.size());
		
			
		// now just test outputting all of the class information
	//	for(int y=0; y<ctagfileInfoList.size();y++){
	//		System.out.println(ctagfileInfoList.get(y).toString());	
	//	}
	}
	
	
	// Get the method name from a line generated by ctag
	private String getMethodFromLine(String input){
		return input.split("\t")[0];
	}

	
	// Determine if line in the ctag file is method
	private boolean isCtagLineMethod(String input){
		boolean retVal=false;
		
		if(!input.startsWith("!")){
			retVal=true;
		}
		
		return retVal;
	}
	
	
	
	
	
	
	// Now sure how to refactor this so it is more reusable?
	private void listFilesAndFilesSubDirectories(String directoryName, String endsWith){

	//	System.out.println("Input Path:" + directoryName);
		
		if(!isDirectoryExist(directoryName)){
			System.out.println("Directory: " + directoryName + " not found\n System Exiting");
			System.exit(1);
		}
		
		
		
		File directory = new File(directoryName);
		//get all the files from a directory
		File[] fList = directory.listFiles();
		for (File file : fList){
			if (file.isFile() && file.getAbsolutePath().endsWith(endsWith)){
		//		System.out.println(file.getAbsolutePath());
				filelist.add(file.getAbsolutePath());
			} else if (file.isDirectory()){
				listFilesAndFilesSubDirectories(file.getAbsolutePath(),endsWith);
		}
		}
		
	}
	
	// Determine if a directory exists
	public boolean isDirectoryExist(String directoryName){
		File f = new File(directoryName); 
		if (!f.isDirectory()) {
			return false;
		}else{
			return true;
		}
	}
	
	
	
	

}
